package com.youga.xuapplication.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.youga.xuapplication.R;
import com.youga.xuapplication.app.XuApplication;
import com.youga.xuapplication.model.User;
import com.youga.xuapplication.ui.BaseActivity;
import com.youga.xuapplication.ui.fragment.CalculatorFragment;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends BaseActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.nav_view)
    NavigationView navView;
    @Bind(R.id.drawer_layout)
    DrawerLayout drawerLayout;
    private ImageView headImg;
    private TextView userName, userCreateDate;
    private User mUser;
    private View headerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        setSupportActionBar(toolbar);
        init();
    }

    private void init() {
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawerLayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawerLayout.setDrawerListener(toggle);
        toggle.syncState();
        navView.setNavigationItemSelectedListener(this);
        headerView = navView.getHeaderView(0);
        headImg = (ImageView) headerView.findViewById(R.id.imageView);
        userName = (TextView) headerView.findViewById(R.id.username);
        userCreateDate = (TextView) headerView.findViewById(R.id.user_createDate);
        mUser = XuApplication.application.getUser();
        userName.setText(mUser.getUsername());
        String[] split = mUser.getCreateDate().split("-");
        userCreateDate.setText("从" + split[0] + "年" + split[1] + "月" + "开始使用" + getString(R.string.app_name));


        headerView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
    }


    @OnClick({R.id.fab})
    public void OnClick(View view) {
        switch (view.getId()) {
            case R.id.fab:
                Intent intent = new Intent(MainActivity.this, AddTourActivity.class);
                startActivity(intent);
                break;
        }

    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.nav_cloud:
                break;
            case R.id.nav_gift:
                break;
            case R.id.nav_calculator:
                CalculatorFragment fragment = CalculatorFragment.newInstance();
                getSupportFragmentManager().beginTransaction()
                        .replace(R.id.container, fragment).commit();
                break;
            case R.id.nav_setting:
                break;
            case R.id.nav_share:
                break;
            case R.id.nav_send:
                break;
            case R.id.nav_help:

                break;

        }
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
}
